<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Shopwindow;
use App\Models\Goods;
use App\Models\User;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class ShopwindowController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Shopwindow(), function (Grid $grid) {
            $grid->model()->with('user')->with('goods')
                ->orderByDesc('id');

            $grid->column('user.tiktokno', trans('user.fields.tiktokno'))->display(function ($var){
                return $this->user_id."({$var})";
            });
            $grid->column('goods.name', trans('good.fields.name'))->width(300);
            //$grid->column('goods.img', trans('good.fields.img'))->image('',50,50);
            $grid->column('goods.original_price', trans('good.fields.original_price'))->display(function ($var) {
                return $var / 100;
            });
            $grid->column('goods.profit', trans('good.fields.profit'))->display(function ($var) {
                return $var / 100;
            });
            $grid->column('shelves')->switch();
            $grid->column('added_time');
            $grid->quickSearch(['user_id','user.tiktokno', 'goods.name']);

            $grid->filter(function (Grid\Filter $filter) {
                //$filter->equal('id');
                $filter->like('user_id', trans('user.fields.id'));
                $filter->equal('user.tiktokno',trans('user.fields.tiktokno'));

            });

            $grid->disableViewButton();
            $grid->showQuickEditButton();

        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Shopwindow(), function (Show $show) {
            $show->field('id');
            $show->field('user_id', trans('user.fields.tiktokno'));
            $show->field('goods_id', trans('good.fields.name'));
            $show->field('shelves');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Shopwindow(), function (Form $form) {
            $form->select('user_id', trans('user.fields.tiktokno'))->options(User::getUsers())->required();
            $form->select('goods_id', '商品名称')->required()->options(Goods::getGoods())->required();
            $form->switch('shelves')->default(1)->required();
            $form->hidden('added_time');

            $form->saving(function (Form $form) {
                $user_id = $form->input('user_id');
                $goods_id = $form->input('goods_id');
                if(\App\Models\Shopwindow::query()->where('user_id','=',$user_id)->where('goods_id','=',$goods_id)->exists()){
                    return $form->response()->error('商品已在橱窗中');
                }

                // 判断是否是新增操作
                if ($form->isCreating()) {
                    $form->added_time = now();
                }
            });

        });
    }




}
